package com.itheima.controller;

import com.itheima.common.entity.Result;
import com.itheima.file.ExcelUtil;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.IOrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Autowired
    private IOrderSettingService orderSettingService;

    @GetMapping("/download")
    public void download(@RequestParam(value = "filename", required = false) String filename, HttpServletResponse response, HttpServletRequest request) {
        try {
            List<String> strings = orderSettingService.generateCurrentMonthDateTemplate();
            if(strings == null || strings.size() == 0){
                return;
            }
            new ExcelUtil().export(strings,response);
        } catch (Exception e) {
            log.error("文件处理异常!");
            e.printStackTrace();
        }
    }

    @PostMapping("/upload")
    public Result upload(@RequestBody MultipartFile excelFile) {
        return orderSettingService.upload(excelFile);
    }

    @PostMapping("/editNumberByOrderDate")
    public Result editNumberByOrderDate(@RequestBody OrderSetting orderSetting){
        return orderSettingService.editNumberByOrderDate(orderSetting);
    }

    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(@RequestParam("month") String date){
        return orderSettingService.getOrderSettingByMonth(date);
    }
}
